package com.ruoyi.human.controller;

import java.util.Date;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.human.domain.HumanClinicalTrial;
import com.ruoyi.human.domain.Participant;
import com.ruoyi.human.domain.ParticipantHealthMonitoring;
import com.ruoyi.human.service.IHumanClinicalTrialService;
import com.ruoyi.human.service.IParticipantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.human.domain.AdverseEventReport;
import com.ruoyi.human.service.IAdverseEventReportService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 不良事件报告Controller
 * 
 * @author gw
 * @date 2025-01-06
 */
@RestController
@RequestMapping("/report")
public class AdverseEventReportController extends BaseController
{
    @Autowired
    private IAdverseEventReportService adverseEventReportService;
    @Autowired
    private IParticipantService participantService;
    @Autowired
    private IHumanClinicalTrialService humanClinicalTrialService;
    /**
     * 查询不良事件报告列表
     */
    @RequiresPermissions("human:report:list")
    @GetMapping("/list")
    public TableDataInfo list(AdverseEventReport adverseEventReport)
    {
        startPage();
        List<AdverseEventReport> list = adverseEventReportService.selectAdverseEventReportList(adverseEventReport);
        for (AdverseEventReport adverseEventReport1:list){
            Long trialId=adverseEventReport1.getTrialId();
            HumanClinicalTrial humanClinicalTrial=humanClinicalTrialService.selectHumanClinicalTrialByTrialId(trialId);
            if(humanClinicalTrial!=null){
                adverseEventReport1.setTrialName(humanClinicalTrial.getTrialName());
            }
        }
        for (AdverseEventReport adverseEventReport1:list){
            Long participantId=adverseEventReport1.getParticipantId();
            Participant participant=participantService.selectParticipantByParticipantId(participantId);
            if(participant!=null){
                adverseEventReport1.setParticipantName(participant.getParticipantName());
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出不良事件报告列表
     */
    @RequiresPermissions("human:report:export")
    @Log(title = "不良事件报告", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AdverseEventReport adverseEventReport)
    {
        List<AdverseEventReport> list = adverseEventReportService.selectAdverseEventReportList(adverseEventReport);
        ExcelUtil<AdverseEventReport> util = new ExcelUtil<AdverseEventReport>(AdverseEventReport.class);
        util.exportExcel(response, list, "不良事件报告数据");
    }

    /**
     * 获取不良事件报告详细信息
     */
    @RequiresPermissions("human:report:query")
    @GetMapping(value = "/{eventId}")
    public AjaxResult getInfo(@PathVariable("eventId") Long eventId)
    {
        return success(adverseEventReportService.selectAdverseEventReportByEventId(eventId));
    }

    /**
     * 新增不良事件报告
     */
    @RequiresPermissions("human:report:add")
    @Log(title = "不良事件报告", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AdverseEventReport adverseEventReport)
    {
        adverseEventReport.setCreatedAt(new Date());
        adverseEventReport.setCreatedBy(SecurityUtils.getUsername());
        return toAjax(adverseEventReportService.insertAdverseEventReport(adverseEventReport));
    }

    /**
     * 修改不良事件报告
     */
    @RequiresPermissions("human:report:edit")
    @Log(title = "不良事件报告", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AdverseEventReport adverseEventReport)
    {
        adverseEventReport.setUpdatedBy(SecurityUtils.getUsername());
        adverseEventReport.setUpdatedAt(new Date());
        adverseEventReport.setVersion(adverseEventReport.getVersion()+1);
        return toAjax(adverseEventReportService.updateAdverseEventReport(adverseEventReport));
    }

    /**
     * 删除不良事件报告
     */
    @RequiresPermissions("human:report:remove")
    @Log(title = "不良事件报告", businessType = BusinessType.DELETE)
	@DeleteMapping("/{eventIds}")
    public AjaxResult remove(@PathVariable Long[] eventIds)
    {
        for (Long id:eventIds){
            AdverseEventReport adverseEventReport=adverseEventReportService.selectAdverseEventReportByEventId(id);
            adverseEventReport.setDeletedAt(new Date());
            adverseEventReport.setDeletedBy(SecurityUtils.getUsername());
            adverseEventReport.setDeletedFlag(1);
            adverseEventReportService.updateAdverseEventReport(adverseEventReport);
        }
        return toAjax(true);
    }
}
